<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>畅购商城</title>
        <meta name="description" content="畅购商城">
        <meta name="keywords" content="畅购商城">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="/plugins/elementui/index.css">
        <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="/css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>规格列表<small>数据列表</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-button round @click="handleBack()">返回</el-button>
                    <el-button round @click="handleRefresh()">刷新</el-button>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="类型名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage1()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column prop="id" align="center" label="编号"></el-table-column>
                        <el-table-column prop="name" label="参数名称" align="center"></el-table-column>
                        <el-table-column prop="templateName" label="模板" align="center"></el-table-column>
                        <el-table-column prop="options" label="可选值列表" align="center"></el-table-column>
                        <el-table-column prop="seq" label="排序" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.page"
                            :page-size="pagination.size"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>

                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="添加类型" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="规格名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="可选值列表" prop="options">
                                            <el-input v-model="formData.options"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="排序">
                                            <el-select v-model="formData.seq">
                                                <el-option label="1" value="1"></el-option>
                                                <el-option label="11" value="11"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑类型" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="规格名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="可选值列表" prop="options">
                                            <el-input v-model="formData.options"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="排序">
                                            <el-select v-model="formData.seq">
                                                <el-option label="1" value="1"></el-option>
                                                <el-option label="11" value="11"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="/js/vue.js"></script>
    <script src="/plugins/elementui/index.js"></script>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/js/axios-0.18.0.js"></script>
    <script>

        var id=[[${id}]];
        var templateName=`[[${name}]]`;
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  page: 1,//当前页码
					  size:10,//每页显示的记录数
					  total:0,//总记录数
                    queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                    name: [{ required: true, message: '类型名称为必填项', trigger: 'blur' }],
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //vue初始化完成后调用分页查询方法
                this.findPage();
            },
            methods: {

                //编辑
                handleEdit() {
                    //验证
                    this.$refs["dataEditForm"].validate((valid)=>{
                        if (valid){
                            //验证通过
                            axios.put("/wpara",this.formData).then((res)=>{
                                if (res.data.flag){
                                    //修改成功
                                    this.$message({
                                        type:"success",
                                        message:res.data.message
                                    });
                                }else {
                                    //执行失败
                                    this.$message.error(res.data.message);
                                }
                            }).finally(()=>{
                                //成功或者失败都调用分页查询
                                this.findPage();
                                //隐藏编辑窗口
                                this.dialogFormVisible4Edit=false;
                            })
                        } else {
                            //验证不通过
                            this.$.messages.error("表单数据校验失败！");
                            return false;
                        }
                    })
                },

                //添加
                handleAdd () {
                    //进行表单校验
                    this.$refs["dataAddForm"].validate((valid)=>{ //valid是表单校验的结果
                        if (valid){
                            //表单校验通过发送请求
                            axios.post("/wpara?id="+id,this.formData).then((res)=>{
                                    //关闭悬浮窗口
                                    this.dialogFormVisible=false;
                                if (res.data.flag){
                                    //新增成功后 重新调用查询方法
                                    this.findPage();
                                    //弹出成功信息
                                    this.$message({
                                        message:res.data.message,
                                        type:"success"
                                    })
                                }else {
                                    //执行失败
                                    this.$message.error(res.data.message);
                                }
                            })
                        }else {
                            //校验失败
                            this.$message.error("校验失败，请检查输入是否正确！");
                            return false;
                        }
                    })
                },

                //分页查询
                findPage() {
                    //发送异步请求，提交分页相关请求参数
                    var param={
                        templateId: id,
                        templateName: templateName,
                        page: this.pagination.page,
                        size: this.pagination.size,
                        name: this.pagination.queryString
                    };

                    axios.post("/wpara/search",param).then((res)=>{

                        if (res.data.flag){
                            this.pagination.total=res.data.data.total;
                            this.dataList=res.data.data.rows;
                        }
                    })
                },

                //条件查询
                findPage1(){
                    this.pagination.currentPage=1;
                    this.findPage();
                },

                // 重置表单
                resetForm() {
                    //重置数据
                    this.formData={};
                },

                // 弹出添加窗口
                handleCreate() {
                    //弹出新增窗口
                    this.dialogFormVisible=true;
                    //调用重置数据
                    this.resetForm();
                },

                // 弹出编辑窗口
                handleUpdate(row) {
                    //弹出编辑窗口
                    this.dialogFormVisible4Edit=true;
                    //发现异步请求 回显数据
                    axios.get("/wpara/"+row.id).then((res)=>{
                        if (res.data.flag){
                            //model回显数据
                            this.formData=res.data.data;
                        }else {
                            //查询失败
                            this.$message.error(res.data.message)
                        }
                    })

                },

                //切换页码
                handleCurrentChange(currentPage) {
                    //设置新页码
                    this.pagination.currentPage=currentPage;
                    //重新调用findPage方法进行分页查询
                    this.findPage();
                },

                // 删除
                handleDelete(row) {
                    //根据id删除检查项
                    this.$confirm("确定要删除当前数据吗？","提示",{
                        type:"warning"
                    }).then(()=>{
                        //点击确定 发送异步请求删除
                        axios.delete("/wpara/"+row.id).then((res)=>{
                            if (res.data.flag){
                                //执行成功
                                this.$message({
                                    type:"success",
                                    message:res.data.message
                                });
                                //查询
                                this.findPage()
                            } else {
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        }).catch((res)=>{
                            this.showMessage(res);
                        })
                    }).catch(()=>{
                        this.$message({
                            type:"info",
                            message:"操作已取消"
                        });
                    });
                },
                // 刷新按钮
                handleRefresh() {
                    location.reload();
                },
                //返回按钮
                handleBack(){
                    window.history.go(-1);
                }
            }
        })
    </script>
</html>